//
//  GTRaiderHeaderView.m
//  GiftTalk
//
//  Created by Wan on 16/4/7.
//  Copyright © 2016年 Wan. All rights reserved.
//  头部滚动控件

#import "GTRaiderHeaderView.h"
#import <View+MASAdditions.h>
#import "UIImageView+Extension.h"
#import "GTRaidersCollection.h"

@interface GTRaiderHeaderView ()
/** 查看全部按钮 */
@property (weak, nonatomic) UIButton *checkAllButton;
/** 滚动条 */
@property (weak, nonatomic) UIScrollView *scrollView;
/** 记录上一个图片 */
@property (weak, nonatomic) UIImageView *previousImageView;
@end

@implementation GTRaiderHeaderView

+ (instancetype)raiderHeaderView {
    return [[self alloc] init];
}

- (instancetype)init {
    if (self = [super init]) {
        UILabel *titleLabel = [[UILabel alloc] init];
        [self addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(10);
            make.height.equalTo(@25);
        }];
        titleLabel.text = @"专题";
        [titleLabel setTintColor:[UIColor blackColor]];
        titleLabel.font = [UIFont systemFontOfSize:16.0];
        
        UIButton *checkAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkAllButton setTitle:@"查看全部>" forState:UIControlStateNormal];
        [checkAllButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [checkAllButton addTarget:self action:@selector(checkAllButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        checkAllButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:checkAllButton];
        [checkAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titleLabel);
            make.right.equalTo(self.mas_right).offset(-10);
            make.bottom.equalTo(titleLabel.mas_bottom);
        }];
        self.checkAllButton = checkAllButton;
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(checkAllButton.mas_bottom).offset(10);
            make.left.right.bottom.equalTo(self);
        }];
        
        self.scrollView = scrollView;
    }
    
    return self;
}

- (void)setCollections:(NSArray *)collections {
    _collections = collections;
    
    for (int i = 0; i < collections.count; i++) {
        // 添加子控件
        GTRaidersCollection *collection = collections[i];
        GTRaiderHeaderImageView *imageView = [[GTRaiderHeaderImageView alloc] init];
        imageView.layer.cornerRadius = 5;
        imageView.clipsToBounds = YES;
        imageView.collection = collection;
        imageView.userInteractionEnabled = YES;
        [self.scrollView addSubview:imageView];
        [imageView setImage:collection.banner_image_url placeHolder:@"placeholder"];
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageviewDidClick:)];
        [imageView addGestureRecognizer:tap];
        
        // 布局子控件
        if (i == 0) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.scrollView);
                make.left.equalTo(self.scrollView).offset(10);
                make.width.equalTo(imageView.mas_height).multipliedBy(2);
                make.height.equalTo(self.scrollView.mas_height).offset(-10);
            }];
        } else if ( i > 0 && i < collections.count - 1) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.previousImageView.mas_right).offset(10);
            make.top.equalTo(self.scrollView); 
                make.width.equalTo(imageView.mas_height).multipliedBy(2);
                make.height.equalTo(self.scrollView.mas_height).offset(-10);
            }];
        } else {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.scrollView);
                make.left.equalTo(self.previousImageView.mas_right).offset(10);
                make.right.equalTo(self.scrollView).offset(-10);
                make.width.equalTo(imageView.mas_height).multipliedBy(2);
                make.height.equalTo(self.scrollView.mas_height).offset(-10);
            }];
        }
        
        // 不是最后一张图片，则记录上一张
        if (i != collections.count) {
            self.previousImageView = imageView;
        }
    
    }
}

/**
 *  监听手势点击图片方法
 */
- (void)imageviewDidClick:(UITapGestureRecognizer *)tap {
    GTRaiderHeaderImageView *headView = (GTRaiderHeaderImageView *)tap.view;
    if ([self.delegate respondsToSelector:@selector(raiderHeaderViewDidClickWithCollection:)]) {
        [self.delegate raiderHeaderViewDidClickWithCollection:headView.collection];
    }
}

- (void)checkAllButtonClicked {
    if ([self.delegate respondsToSelector:@selector(raiderHeaderViewDidClickCheckAllButton:)]) {
        [self.delegate raiderHeaderViewDidClickCheckAllButton:self];
    }
}

@end

@implementation GTRaiderHeaderImageView : UIImageView

@end
